using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;

namespace Game.Utils
{
	public class Compressor
	{
		public static byte[] Compress(string val)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(val);
			return Compress(bytes);
		}

		public static byte[] Compress(byte[] bytes)
		{
			MemoryStream memoryStream = new MemoryStream();
			GZipOutputStream gZipOutputStream = new GZipOutputStream(memoryStream);
			gZipOutputStream.Write(bytes, 0, bytes.Length);
			gZipOutputStream.Close();
			return memoryStream.ToArray();
		}

		public static string CompressToString(byte[] bytes)
		{
			return Convert.ToBase64String(Compress(bytes));
		}

		public static string UnCompress(byte[] bytes)
		{
			return Encoding.UTF8.GetString(Uncompress(bytes));
		}

		public static byte[] Uncompress(byte[] bytes)
		{
			MemoryStream memoryStream = new MemoryStream();
			MemoryStream memoryStream2 = new MemoryStream(bytes);
			GZipInputStream gZipInputStream = new GZipInputStream(memoryStream2);
			byte[] array = new byte[1024];
			int num = 0;
			while ((num = gZipInputStream.Read(array, 0, array.Length)) != 0)
			{
				memoryStream.Write(array, 0, num);
			}
			memoryStream2.Dispose();
			memoryStream2.Close();
			gZipInputStream.Close();
			return memoryStream.ToArray();
		}

		public static byte[] UncompressToBytes(string val)
		{
			return Uncompress(Convert.FromBase64String(val));
		}
	}
}
